import { NextRequest } from "next/server";
import { createAuthProxyHandler, API_PROXY_CONFIGS } from "@/utils/api-proxy";

// 统一的API代理处理器
export async function GET(request: NextRequest) {
  return handleApiRequest(request, "GET");
}

export async function POST(request: NextRequest) {
  return handleApiRequest(request, "POST");
}

export async function PUT(request: NextRequest) {
  return handleApiRequest(request, "PUT");
}

export async function DELETE(request: NextRequest) {
  return handleApiRequest(request, "DELETE");
}

// 处理API请求的通用函数
async function handleApiRequest(request: NextRequest, method: string) {
  const { pathname } = new URL(request.url);
  const path = pathname.split("/api/")[1];

  // 根据路径选择对应的配置
  let config;
  switch (path) {
    case "favorite":
      config = API_PROXY_CONFIGS.favorite;
      break;
    case "favorite/remove":
      config = API_PROXY_CONFIGS.favoriteRemove;
      break;
    case "wechat":
      config = API_PROXY_CONFIGS.wechatCode;
      break;
    case "oss":
      config = API_PROXY_CONFIGS.oss;
      break;
    case "application":
      config = API_PROXY_CONFIGS.application;
      break;
    default:
      return new Response("API not found", { status: 404 });
  }

  const handler = createAuthProxyHandler(config.baseUrl, {
    requireAuth: config.requireAuth,
  });

  return handler(request);
}
